﻿using FRMS.BUS;
using FRMS.DAL;
using FRMS.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace FRMS.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class VoucherController : ControllerBase
    {
        [Obsolete]
        private readonly IHostingEnvironment _hostingEnvironment;
        private JwtSettings _jwtSettings;
        IHttpContextAccessor _access;
        private IHttpClientFactory _httpClient;
        public VoucherController(IHostingEnvironment hostingEnvironment, IOptions<JwtSettings> jwtSettingsAccesser, IHttpContextAccessor access, IHttpClientFactory httpClient
          )
        {
            _hostingEnvironment = hostingEnvironment;
            JwtSettings jwt = new JwtSettings();
            jwt.Audience = AppSettingsHelper.Configuration["JwtSettings:Audience"];
            jwt.Issuer = AppSettingsHelper.Configuration["JwtSettings:Issuer"];
            jwt.SecretKey = AppSettingsHelper.Configuration["JwtSettings:SecretKey"];
            _jwtSettings = jwt;
            _access = access;
            _httpClient = httpClient;
        }

        /// <summary>
        /// 凭证删除
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("VoucherDel")]
        public ResponseMessage VoucherDel(VoucherInfo voucherInfo)
        {
            ResponseMessage message = new ResponseMessage();
            VoucherBUS BUS = new VoucherBUS();
            try
            {
                message = BUS.DelVoucher(voucherInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "删除凭证";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "删除异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 凭证绑定
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("BindVoucher")]
        public ResponseMessage VoucherBind(VoucherInfo voucherInfo)
        {
            ResponseMessage message = new ResponseMessage();
            VoucherBUS BUS = new VoucherBUS();
            try
            {
                string path = _hostingEnvironment.ContentRootPath;
                voucherInfo.projectID = Convert.ToInt32(_access.HttpContext.Session.GetString("ProjectID"));
                message = BUS.AddVoucher(voucherInfo, path);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "绑定凭证";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "凭证绑定失败";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 查询凭证
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryVoucher")]
        public ResponseMessage QueryVoucher(VoucherInfo voucherInfo)
        {
            ResponseMessage message = new ResponseMessage();
            VoucherBUS BUS = new VoucherBUS();
            try
            {
                voucherInfo.projectID = Convert.ToInt32(_access.HttpContext.Session.GetString("ProjectID"));
                message = BUS.QueryVoucher(voucherInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "查询凭证";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "查询凭证异常";
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 载入凭证
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("LoadVoucher")]
        public ResponseMessage LoadVoucher(VoucherInfo voucherInfo)
        {
            ResponseMessage message = new ResponseMessage();
            VoucherBUS BUS = new VoucherBUS();
            try
            {
                message = BUS.LoadVoucher(voucherInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "载入凭证";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "载入凭证失败";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 巨龙凭证下发
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("JLVoucherIssue")]
        public ResponseMessage JLVoucherIssue(int id, string deviceip)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                string path = _hostingEnvironment.ContentRootPath;
                VoucherBUS BUS = new VoucherBUS();

                message = BUS.JLVoucherIssue(id, deviceip, path);
                
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "凭证下发";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "下发异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 巨龙凭证清除所有
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("JLVoucherClear")]
        public ResponseMessage JLVoucherClear(int deviceid, int personIdentity)
        {
            ResponseMessage message = new ResponseMessage();
            VoucherBUS BUS = new VoucherBUS();
            try
            {
                if (personIdentity==0)
                {
                    message = BUS.JLVoucherClearAll(deviceid, personIdentity);
                }
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "凭证清空";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "清空异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 巨龙凭证批量清除
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("JLVoucherBatchDel")]
        public ResponseMessage JLVoucherBatchDel(List<JLLssueinfoDelBatchPerson> list, int deviceid)
        {
            ResponseMessage message = new ResponseMessage();
            VoucherBUS BUS = new VoucherBUS();
            DeviceInfo deviceInfo = new DeviceInfo();
            try
            {
                message = BUS.DeleteBatchVoucher(list, deviceid);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "凭证批量清除";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "凭证批量清除异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 查询访客凭证
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryVisitVoucher")]
        public ResponseMessage QueryVisitVoucher(VoucherInfo voucherInfo)
        {
            ResponseMessage message = new ResponseMessage();
            VoucherBUS BUS = new VoucherBUS();
            try
            {
                voucherInfo.projectID = Convert.ToInt32(_access.HttpContext.Session.GetString("ProjectID"));
                voucherInfo.OwnerType = 4;
                message = BUS.QueryVoucher(voucherInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "查询访客凭证";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "查询访客凭证异常";
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 新增物业凭证
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("AddProVoucher")]
        public ResponseMessage AddProVoucher(OwnerDeviceVoucher ownerDevice)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                VoucherBUS BUS = new VoucherBUS();
                message = BUS.AddProVoucher(ownerDevice);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "新增物业凭证";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "新增物业凭证异常";
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 凭证一键下发
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("VoucherIssueOneClick")]
        public ResponseMessage VoucherIssueOneClick(ProVoucherInfo proVoucher)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                VoucherBUS BUS = new VoucherBUS();
                string path = _hostingEnvironment.ContentRootPath;
                message = BUS.OneClickVoucherIssue(proVoucher, path);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "一键下发凭证";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "一键下发凭证异常";
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 凭证一键删除
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("VoucherDeleteOneClick")]
        public ResponseMessage VoucherDeleteOneClick(ProVoucherInfo proVoucher)
        {

            ResponseMessage message = new ResponseMessage();
            try
            {
                VoucherBUS BUS = new VoucherBUS();
                message = BUS.OneClickVoucherDel(proVoucher);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "一键删除凭证异常";
                message.total = 0;
            }
            return message;
        }


        [HttpPost]
        [Route("ToBase64String")]
        public string ToBase64String(string Imagefilename)
        {
            try
            {
                Bitmap bmp = new Bitmap(Imagefilename);

                MemoryStream ms = new MemoryStream();
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                ms.Close();
                return Convert.ToBase64String(arr);
            }
            catch (Exception ex)
            {
                return "";
            }
        }
    }
}
